﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace UrlForm2
{
    public partial class Form1 : Form
    {
        SimpleCrawler Crawl = new SimpleCrawler(); 
        public Form1()
        {
            InitializeComponent();
            Crawl.PageDownloaded += Crawler_PageDownloaded;
        }

        private void Crawler_PageDownloaded(string obj)
        {
            if (this.listBox1.InvokeRequired)
            {
                Action<String> action = this.AddUrl;
                this.Invoke(action, new object[] { obj });
            }
            else
            {
                listBox1.Items.Add(obj);
            }
        }

        private void AddUrl(string url)
        {
            listBox1.Items.Add(url);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(StartUrlTxt.Text != "")
            {
                Crawl.startUrl = StartUrlTxt.Text;
                listBox1.Items.Clear();
                new Thread(Crawl.Crawl).Start();
            }
        }
    }
}
